/**
 * flyLine.js
 * Version: 1.0
 *
 */
var flyLine = function () {

    var echartsFlightLayer = null; 
    /** 飞线函数
     * @param {*} geoCoordMap[]所有经纬度  '长春': [125.8154, 44.2584]
     * @param {*} data [['北京', BJData], ['上海', SHData], ['广州', GZData]]  例：BJData[]  [{name: '上海'}, {name: '包头', value: 93}]
     * @param {*} color ['#a6c84c', '#46bee9', '#ff3399']
     */
    function getFlyLine(geoCoordMap,flyData,color) {
        var convertData = function (data) {
            var res = [];
            for (var i = 0; i < data.length; i++) {
                var dataItem = data[i];
                var fromCoord =geoCoordMap[dataItem[0].name];
                var toCoord = geoCoordMap[dataItem[1].name];
                if (fromCoord && toCoord) {
                    res.push({
                        fromName: dataItem[0].name,
                        toName: dataItem[1].name,
                        coords: [fromCoord, toCoord]
                    });
                }
            }
            return res;
        };
        var color = color ;
        var series = [];
        flyData.forEach(
            function (item, i) {
                series.push({
                        name: item[0],
                        type: 'lines',
                        zlevel: 1,
                        effect: {
                            show: true,
                            period: 6,
                            trailLength: 0.7,
                            color: '#fff',
                            symbolSize: 3
                        },
                        lineStyle: {
                            normal: {
                                color: color[i],
                                width: 0,
                                curveness: 0.2
                            }
                        },
                        data: convertData(item[1])
                    },
                    {
                        name: item[0],
                        type: 'lines',
                        zlevel: 2,
                        effect: {
                            show: true,
                            period: 6,
                            trailLength: 0,
                            //symbol: planePath,
                            symbol: 'circle',
                            symbolSize: 6
                        },
                        lineStyle: {
                            normal: {
                                color: color[i],
                                width: 1,
                                opacity: 0.4,
                                curveness: 0.2
                            }
                        },
                        data: convertData(item[1])
                    },
                    {
                        name: item[0],
                        type: 'effectScatter',
                        coordinateSystem: 'geo',
                        zlevel: 2,
                        rippleEffect: {
                            brushType: 'stroke'
                        },
                        label: {
                            normal: {
                                show: true,
                                position: 'right',
                                formatter: '{b}'
                            }
                        },
                        symbolSize: function (val) {
                            return val[2] / 8;
                        },
                        itemStyle: {
                            normal: {
                                color: color[i]
                            }
                        },
                        data: item[1].map(function (dataItem) {
                            return {
                                name: dataItem[1].name,
                                value: geoCoordMap[dataItem[1].name].concat([dataItem[1].value])
                            };
                        })
                    });
            });
        return {
            tooltip: {
                trigger: 'item'
            },
            series: series
        };
    }


    this.showFlyLine  = function (geoCoordMap,flyData,color){
        var option = getFlyLine(geoCoordMap,flyData,color);
        echartsFlightLayer = new ol3Echarts(option);
        console.log(map);
        echartsFlightLayer.appendTo(map);
    };

    function removeFlyLine(){
        echartsFlightLayer.remove();
    }



}


